/*
* @author : TF-BJ-C064
* @creation : 2014-7-28 下午4:59:27
* @description : 
*
*/

package com.util;

import java.util.Map;
import java.util.StringTokenizer;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * 浏览器信息
 * <p>Title: Browser</p>
 * <p>Description: </p>
 * <p>Company: www.teleframe.cn</p> 
 * @author	lijian
 * @date	2014-7-28
 */
public class Browser implements java.io.Serializable{
	
	/** serialVersionUID*/
	private static final long serialVersionUID = -3986809130664426478L;
	
	private String sessionId;
	private HttpSession session;
	private String userAgent;
	private String cookie;
	private String name;
	private String version;
	private String referer;
	private String host;
	private String ip;
	
	
	public final static String IE9="MSIE 9.0";  
	public final static String IE8="MSIE 8.0";  
	public final static String IE7="MSIE 7.0";  
	public final static String IE6="MSIE 6.0";  
	public final static String MAXTHON="Maxthon";  
	public final static String QQ="QQBrowser";  
	public final static String GREEN="GreenBrowser";  
	public final static String SE360="360SE";  
	public final static String FIREFOX="Firefox";  
    public final static String OPERA="Opera";  
    public final static String CHROME="Chrome";  
    public final static String SAFARI="Safari";  
    public final static String OTHER="other";  
    
	
	public Browser(){}
	
	public String checkBrowse(String userAgent){  
        if(regex(OPERA, userAgent))return OPERA;  
        if(regex(CHROME, userAgent))return CHROME;  
        if(regex(FIREFOX, userAgent))return FIREFOX;  
        if(regex(SAFARI, userAgent))return SAFARI;  
        if(regex(SE360, userAgent))return SE360;  
        if(regex(GREEN,userAgent))return GREEN;  
        if(regex(QQ,userAgent))return QQ;  
        if(regex(MAXTHON, userAgent))return MAXTHON;  
        if(regex(IE9,userAgent))return IE9;  
        if(regex(IE8,userAgent))return IE8;  
        if(regex(IE7,userAgent))return IE7;  
        if(regex(IE6,userAgent))return IE6;  
        return OTHER;  
    }  
    public boolean regex(String regex,String str){  
        Pattern p =Pattern.compile(regex,Pattern.MULTILINE);  
        Matcher m=p.matcher(str);  
        return m.find();  
    }  
	
	public Browser(HttpServletRequest request){
		if(request==null)
			return;
		this.session = request.getSession();
		this.setSessionId(this.session.getId());
		this.userAgent = request.getHeader("user-agent");
		this.referer = request.getHeader("Referer");
		this.host = request.getRemoteHost();
		this.ip = GUtil.parseIp(request, null);
		//Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)|#
		//Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.101 Safari/537.36|#
		this.name = checkBrowse(userAgent);
		
	}

	
	/**
	 * 判断 android、iphone等手机浏览器
	 * <p>Title: JudgeIsMoblie</p>
	 * <p>Description: </p>
	 * @param request
	 * @return  null  不是手机浏览器
	 * 			"..." 返回手机浏览器名称标识
	 */
	public String JudgeIsMoblie(HttpServletRequest request) {
        boolean isMoblie = false;
        String[] mobileAgents = { "iphone", "android", "phone", "mobile", "wap", "netfront", "java", "opera mobi",
                "opera mini", "ucweb", "windows ce", "symbian", "series", "webos", "sony", "blackberry", "dopod",
                "nokia", "samsung", "palmsource", "xda", "pieplus", "meizu", "midp", "cldc", "motorola", "foma",
                "docomo", "up.browser", "up.link", "blazer", "helio", "hosin", "huawei", "novarra", "coolpad", "webos",
                "techfaith", "palmsource", "alcatel", "amoi", "ktouch", "nexian", "ericsson", "philips", "sagem",
                "wellcom", "bunjalloo", "maui", "smartphone", "iemobile", "spice", "bird", "zte-", "longcos",
                "pantech", "gionee", "portalmmm", "jig browser", "hiptop", "benq", "haier", "^lct", "320x320",
                "240x320", "176x220", "w3c ", "acs-", "alav", "alca", "amoi", "audi", "avan", "benq", "bird", "blac",
                "blaz", "brew", "cell", "cldc", "cmd-", "dang", "doco", "eric", "hipt", "inno", "ipaq", "java", "jigs",
                "kddi", "keji", "leno", "lg-c", "lg-d", "lg-g", "lge-", "maui", "maxo", "midp", "mits", "mmef", "mobi",
                "mot-", "moto", "mwbp", "nec-", "newt", "noki", "oper", "palm", "pana", "pant", "phil", "play", "port",
                "prox", "qwap", "sage", "sams", "sany", "sch-", "sec-", "send", "seri", "sgh-", "shar", "sie-", "siem",
                "smal", "smar", "sony", "sph-", "symb", "t-mo", "teli", "tim-", "tosh", "tsm-", "upg1", "upsi", "vk-v",
                "voda", "wap-", "wapa", "wapi", "wapp", "wapr", "webc", "winw", "winw", "xda", "xda-",
                "Googlebot-Mobile" };
        String userAgent = null;
        if (request.getHeader("User-Agent") != null) {
            for (String mobileAgent : mobileAgents) {
                if (request.getHeader("User-Agent").toLowerCase().indexOf(mobileAgent) >= 0) {
                    isMoblie = true;
                    userAgent = mobileAgent;
                    break;
                }
            }
        }
        return userAgent;
    }
	
	public String getSessionId() {
		return sessionId;
	}
	public void setSessionId(String sessionId) {
		this.sessionId = sessionId;
	}
	public String getUserAgent() {
		return userAgent;
	}
	public void setUserAgent(String userAgent) {
		this.userAgent = userAgent;
	}
	public String getCookie() {
		return cookie;
	}
	public void setCookie(String cookie) {
		this.cookie = cookie;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getVersion() {
		return version;
	}
	public void setVersion(String version) {
		this.version = version;
	}
	public String getReferer() {
		return referer;
	}
	public void setReferer(String referer) {
		this.referer = referer;
	}
	public String getHost() {
		return host;
	}
	public void setHost(String host) {
		this.host = host;
	}

	public HttpSession getSession() {
		return session;
	}

	public void setSession(HttpSession session) {
		this.session = session;
	}

	public String getIp() {
		return ip;
	}

	public void setIp(String ip) {
		this.ip = ip;
	}
	
}


